/*
 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lega_rtos.h"
#include "alto_common.h"

#include "asr_rnd.h"
#include "asr_sec_hw_common.h"

#define USE_C310 1
#define MAX_RND_SIZE_VECTOR 0x20

typedef struct rndDataStuct
{
    uint32_t rnd_RandomVectorSize;
    uint8_t  rnd_RandomVectorData[MAX_RND_SIZE_VECTOR];
} asr_rndDataStuct;

asr_rndDataStuct rndVectors = {
    16,
    /*First random vector*/
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
};

#if USE_C310
uint8_t* get_random_int_c()
#else
int get_random_int_c()
#endif
{
#if USE_C310
    uint8_t* random_c = NULL;
    uint32_t ret = 0;

    asr_security_engine_init();

    /*Generate random vector*/
    ret = asr_hal_random_get(rndVectors.rnd_RandomVectorData, rndVectors.rnd_RandomVectorSize);

    if (ret != ASR_SILIB_RET_OK){
        printf("\n RANDOM READ failed \r\n");
        return NULL;
    }

    random_c = rndVectors.rnd_RandomVectorData;

    return random_c;
#else
    int i;
    srand(lega_rtos_get_time());
    i = rand();
    return i;
#endif
}

/**
  * @brief    if no mac address burned in efuse memory,
  *           user could modify this function to set default mac address
  *
  * @param    mac_addr : mac_addr pointer wifi stack used
  */
void lega_wlan_default_mac_addr(uint8_t *mac_addr)
{
#if USE_C310
    uint8_t * addr = get_random_int_c();
    uint8_t addr3 =0x00;
    uint8_t addr4 =0x00;
    uint8_t addr5 =0x00;
    if(addr == NULL)
    {
        printf("get random mac addr fail,wifi_set_mac_addr 8c59dcxxxxxx");
    }
    else
    {
        addr3 =*(addr ++)&0xff;
        addr4 =*(addr ++)&0xff;
        addr5 =*(addr ++)&0xff;
    }
    mac_addr[0] = 0x8C;
    mac_addr[1] = 0x59;
    mac_addr[2] = 0xDC;
    mac_addr[3] = addr3;
    mac_addr[4] = addr4;
    mac_addr[5] = addr5;
#else
    mac_addr[0] = 0x8C;
    mac_addr[1] = 0x59;
    mac_addr[2] = 0xDC;
    mac_addr[3] = (uint8_t)((get_random_int_c()>>16)&0xff);
    mac_addr[4] = (uint8_t)((get_random_int_c()>>8)&0xff);
    mac_addr[5] = (uint8_t)((get_random_int_c()>>0)&0xff);
#endif
}

/*void lega_rf_set_xo_freq_bias(void)
{
#ifdef XO_FREQ_BIAS_CONFIG
    //XO freq bias for blue board
    REG_WR(0x40000A74, 0x9191);
#endif
    printf("Current xo config is 0x%x\r\n",(unsigned int)REG_RD(0x40000A74));
}*/
